<?php

说明:
服务提供者也作为消费者! 消费者同时也是作为提供者!
者如'太极图'一般, 如影相随, 也似'力的相互作用', 施力者同时也是受力者...

---------------------------------------- [Eureka-Server]
1->pom
   这里外层最好定义'父类pom', 详见'附件1-通用pom文件.php'
   <dependencies>
      <!-- eureka服务端 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
   </dependencies>


2->启动类注解
   @EnableEurekaServer
   @SpringBootApplication


3->配置文件
   application.yml
   spring:
      application:
          name: eureka-server

   eureka:
      instance:
          hostname: localhost               # eureka实例的地址
          // lease-expiration-duration-in-seconds: 10 # 服务超过指定秒数没有发起renew请求的话，就会被当做失效服务剔除掉。
          // lease-renewal-interval-in-seconds: 5     # 服务发起renew续约间隔

      client:
          register-with-eureka: false       # 代表是否将[自身注册], 集群打开
          fetch-register: false             # 是否从eureka[拉取注册表]
          // service-uri:                   # 配置[eureka集群], 只要使得: 各自将自己注册上去就行.
            // defaultZone: http://168.60.90.155:9600/eureka/


---------------------------------------- [Eureka-Client]
1->pom
   <dependencies>
      <!-- eureka客户端 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

      <!-- web -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
   </dependencies>


2->启动类注解
   @EnableDiscoveryClient
   @SpringBootApplication


3->配置文件
   application.yml
   spring:
      application:
          name: eureka-client

   eureka:
      instance:
          hostname: localhost               # eureka实例的地址
      client:
        register-with-eureka: false       # 代表是否将[自身注册], 集群打开
        fetch-register: false             # 是否从eureka[拉取注册表]
        service-uri:
          defaultZone: http://localhost:9500/eureka/                          # 单节点配置
          register-with-eureka: false       # 代表是否将[自身注册], 集群打开
          fetch-register: false             # 是否从eureka[拉取注册表]
          // defaultZone: http://peer1:9500/eureka/,http://peer2:9600/eureka/    # 集群配置


注意: 在没有使用'Feign'之前, 各服务之间调用非常麻烦! 需要借助'LoadBalancerClient + restTemplate',
     而且请求别的服务还需要'手动拼接接口地址'!
     LoadBalancerClient: springcloud内部.
     restTemplate: 发起HTTP请求.

     @Autowired
     private RestTemplate restTemplate;
     @Autowired
     private LoadBalancerClient client;

     @GetMapping("getMySelf")
     public String getMySelf() {
        return "My name is " + applicationName;
     }

     @GetMapping("callOtherService")
     public String callOtherService() {
        ServiceInstance instance = client.choose("eureka-client");
        String url = String.format("http://%s:%s/sayHello", instance.getHost(), instance.getPort());
        return restTemplate.getForObject(url, String.class);
     }
